# Created by 黄景涛
# DATE 2024/7/17
import glob
import os
import re
import yaml


def modify_yaml_files(directory, old_val='/o/cc-portal_saas', new_val=''):
    # 查找目录下所有的 YAML 文件
    yaml_files = glob.glob(os.path.join(directory, '**', '*.yaml'), recursive=True)

    for yaml_file in yaml_files:
        with open(yaml_file, 'r', encoding='utf8') as f:
            data = f.read()
        print(f'yaml文件：{yaml_file}')

        # 修改特定的键的值
        pattern = old_val
        new_data = re.sub(pattern, new_val, data)
        # 将修改后的数据写回 YAML 文件
        with open(yaml_file, 'w', encoding='utf8') as f:
            f.write(new_data)


if __name__ == "__main__":
    # 指定目录路径
    directory = r'D:\hjt_workspace\my_doc\edite_files\testcases'
    modify_yaml_files(directory)
